package Inet;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class TestPort {
    public static void main(String[] args) throws UnknownHostException {
        //获取本机IP
        InetAddress ip = InetAddress.getLocalHost();// ADMINISTRATOR/192.xxx.xxx.xxx
        System.out.println(ip);
        //获得主机名
        System.out.println(ip.getHostName());// ADMINISTRATOR
        //获得IP地址
        System.out.println(ip.getHostAddress());// 192.xxx.xxx.xxx
        //getLocalHost=getHostName+getHostAddress
        System.out.println(InetAddress.getByName("localhost"));
        InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
        // 获取此 IP 地址的主机名。
        System.out.println(inetAddress.getHostName());
        //返回 IP 地址字符串（以文本表现形式）。
        System.out.println(inetAddress.getHostAddress());
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",8082);
        System.out.println(inetSocketAddress);
        System.out.println(inetSocketAddress.getHostName());
        InetAddress inetAddress2 = inetSocketAddress.getAddress();
        Inet4Address inet4Address = (Inet4Address) inetSocketAddress.getAddress();
        System.out.println(inet4Address);
        System.out.println(inetAddress2);

    }
}
